import 'package:flutter/cupertino.dart';
import 'package:x3_component_core/models/base_bottom_sheet_selector_model.dart';

class BaseBottomSheetController extends ChangeNotifier {
  /// 数据类
  List<BaseBottomSheetSelectorModel> items;

  /// 提示语
  String hit;

  /// 选择中值
  BaseBottomSheetSelectorModel _selectedItem;
  BaseBottomSheetSelectorModel get selectedItem => _selectedItem;
  set selectedItem(dynamic value) {
    if (value is BaseBottomSheetSelectorModel) {
      _selectedItem = value;
      return;
    }
    if (value is Map<String, dynamic>) {
      _selectedItem = BaseBottomSheetSelectorModel.fromJson(value);
      return;
    }
    _selectedItem = BaseBottomSheetSelectorModel(key: null, value: null);
  }

  BaseBottomSheetController({@required this.items, BaseBottomSheetSelectorModel selectedItem, String hit}) : assert(items != null) {
    if (selectedItem == null) {
      this.selectedItem = BaseBottomSheetSelectorModel(key: null, value: null);
    } else {
      this.selectedItem = selectedItem;
    }

    if (hit == null || hit.isNotEmpty) {
      this.hit = '请选择';
    } else {
      this.hit = hit;
    }
  }

  /// 工厂模式构建控制器
  factory BaseBottomSheetController.formJson(List<Map> json, {Map<String, dynamic> selectedItem, String hit}) {
    List<BaseBottomSheetSelectorModel> _items = BaseBottomSheetSelectorModel.getListBaseSelectDownItem(json);
    BaseBottomSheetSelectorModel _selectedItem = BaseBottomSheetSelectorModel.fromJson(selectedItem);
    return BaseBottomSheetController(items: _items, selectedItem: _selectedItem, hit: hit);
  }

  /// 弹出底部
  void showBottomSheet() {
    notifyListeners();
  }

  /// 是否选中
  bool isSelected(BaseBottomSheetSelectorModel selectedItem) {
    return this.selectedItem.key.toString() == selectedItem.key.toString();
  }
}
